package com.scy.edu.server;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Path;

/**
 * @author 司晨雨
 * @date 2020/7/8 22:48
 * @description
 */
public class MyClassLoader extends ClassLoader {

    @Override
    protected Class<?> findClass(String contextPath, String className) {
        String myPath = contextPath+ File.separator+ className.replace(".", File.separator) + ".class";
        System.out.println(myPath);
        byte[] cLassBytes = null;
        Path path = null;
        try {
            FileInputStream in = new FileInputStream(myPath) ;
            ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
            byte[] buf = new byte[1024] ;
            int len = -1 ;
            while((len = in.read(buf)) != -1){
                baos.write(buf , 0 , len);
            }
            in.close();
            byte[] classBytes = baos.toByteArray();
            return defineClass(classBytes , 0 , classBytes.length) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
